/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package components;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.Socket;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import processor.MyFile;

/**
 *
 * @author DYNG09
 */
public class Toolbar extends JPanel{
    private ArrayList<JButton> listButton;
    private ClosingTab tabbedPane;
    private MyFile myFile;
    private Socket s;
    
    /**
     * Constructor
     * @param listTab a list manage tabs
     * @param tabbedPane a pane contain tabs
     */
    public Toolbar(ClosingTab tabbedPane, Socket socket){
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setBorder(new EtchedBorder());
        this.tabbedPane = tabbedPane;
        this.s = socket;
        myFile = new MyFile(tabbedPane);
        listButton = new ArrayList();
        LoadToolbar();
        
        for(int i = 0; i < listButton.size(); i++){
            this.add(listButton.get(i));
        }
    }
    
    /**
     * Load toolbar compose the buttons
     */
    private void LoadToolbar(){
        //add button
        addButtonToList(null, "Add new edit screen", "/resources/AddNew_Normal.png", 0);
        addButtonToList(null, "Save edit file", "/resources/Save_Normal.png", 1);
        addButtonToList(null, "Load text file", "/resources/Load_Normal.png", 2);
        //add command
        addActionListener();
        //create new tab
        addTab(tabbedPane, createImageIcon("/resources/edit.png", null), "New Document *", null);
    }
    
    /**
     * add all buttons to a list for managing
     * @param tooltip
     * @param pathImage
     * @param index 
     */
    private void addButtonToList(String content, String tooltip, String pathImage, int index){
        final int i = index;
        JButton b = new JButton(content, new ImageIcon(getClass().getResource(pathImage)));
        
        b.setSize(60, 60);
        b.setFont(new Font("Tahoma", 1, 11));
        b.setHorizontalTextPosition(SwingConstants.LEFT);
        b.setToolTipText(tooltip);
        b.setBorder(null);
        b.setBorderPainted(false);
        listButton.add(b);
        b.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                
            }

            @Override
            public void mousePressed(MouseEvent e) {
                
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                setActive(i);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setNormal(i);
            }
        });
    }
    
    /**
     * set active image for button
     * @param index 
     */
    private void setActive(int index) {
        switch(index){
            case 0:
                listButton.get(index).setIcon(new 
                        ImageIcon(getClass().getResource("/resources/AddNew_Active.png")));
                break;
            case 1:
                listButton.get(index).setIcon(new 
                        ImageIcon(getClass().getResource("/resources/Save_Active.png")));
                break;
            case 2:
                listButton.get(index).setIcon(new 
                        ImageIcon(getClass().getResource("/resources/Load_Active.png")));
                break;
        }
        listButton.get(index).setFont(new Font("Times New Roman", 3, 16));
    }

    /**
     * set normal image for button
     * @param index 
     */
    private void setNormal(int index) {
        switch(index){
            case 0:
                listButton.get(index).setIcon(new 
                        ImageIcon(getClass().getResource("/resources/AddNew_Normal.png")));
                break;
            case 1:
                listButton.get(index).setIcon(new 
                        ImageIcon(getClass().getResource("/resources/Save_Normal.png")));
                break;
            case 2:
                listButton.get(index).setIcon(new 
                        ImageIcon(getClass().getResource("/resources/Load_Normal.png")));
                break;
        }
        listButton.get(index).setFont(new Font("Tahoma", 1, 11));
    }
    
    /**
     * catch event of the buttons
     */
    private void addActionListener(){
        //button add edit screen
        listButton.get(0).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                addTab(tabbedPane, createImageIcon("/resources/edit.png", null), "New Document *", null);
            }
        });
        
        //button save
        listButton.get(1).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                save();
            }
        });
        
        //button load
        listButton.get(2).addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                load();
            }
        });
    }
    
    /**
     * create ImageIcon
     *
     * @param path path of image file
     * @param description a string text to describe
     * @return ImageIcon
     */
    private ImageIcon createImageIcon(String path, String description) {
        URL imgURL = getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    /**
     * Add a tab to tabbedPane
     *
     * @param tabbedPane a top pane for adding tabs
     * @param icon an image icon for this tab
     * @param tabTitle title of this tab
     * @param textHint a string text for tool tip
     */
    private void addTab(ClosingTab tabbedPane, ImageIcon icon, String tabTitle, String textHint) {
        EditTab etp = new EditTab(tabTitle, s);
        tabbedPane.addTab(tabTitle, icon, etp, textHint);
        tabbedPane.setSelectedIndex(tabbedPane.getListTab().size());
        tabbedPane.getListTab().add(etp);
    }
    
    /**
     * save edit text in file with extension .myfile
     */
    private void save(){
        int i = this.tabbedPane.getSelectedIndex();
        myFile.setContent(tabbedPane.getListTab().get(i).getEditArea().getText());
        myFile.saveEditedFile();
        tabbedPane.setTitleAt(i, myFile.getFileChoosed().getName() + ".myfile");
    }
    
    /**
     * load text file with extension .myfile
     */
    private void load(){
        myFile.loadFile();
        //add tab
        addTab(tabbedPane, createImageIcon("/resources/edit.png", null), 
                myFile.getFileChoosed().getName(), null);
        //load text
        int i = this.tabbedPane.getSelectedIndex();
        tabbedPane.getListTab().get(i).getEditArea().setText(myFile.getContent());
    }
}
